﻿using NFine.Application.SystemManage;
using NFine.Code;
using NFine.Domain.Entity.SystemManage;
using System.Web.Mvc;

namespace NFine.Web.Areas.MemberManage.Controllers
{
    [HandlerLogin]
    public class MemberController : ControllerBase
    {
        private UserApp userApp = new UserApp();
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = userApp.GetForm(keyValue);
            return Content(data.ToJson());
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormInitJson()
        {
            var data = userApp.GetFormInit();
            return Content(data.ToJson());
        }

        public ActionResult DeleteForm(string keyValue) {
            userApp.DeleteForm(keyValue);
            return Success("操作成功。");
        }

        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(UserEntity userEntity, UserLogOnEntity userLogOnEntity, string keyValue)
        {
            if (userApp.IsExistDepartmentId(userEntity.F_DepartmentId, userEntity.F_Id))
                return Error("身份证号：“"+userEntity.F_DepartmentId+"”已注册，请重新输入身份证号！");
            userEntity.F_RoleId =  Configs.GetValue("MemberRoleId");
            userApp.SubmitForm(userEntity, userLogOnEntity, keyValue);
            return Success("操作成功。");
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTreeGridJson(Pagination pagination)
        {
            var data = userApp.GetList(pagination);
          
            return Content(data.TreeGridJson());
        }

        [HttpGet]
        public override ActionResult Index()
        {
            var u = OperatorProvider.Provider.GetCurrent();
            if (u.IsOrder)
            {
                var user = userApp.GetForm(u.UserId);
                u.IsOrder = user.F_DutyId != "1";
            }
            ViewData["FirstOrder"] = !(u.IsSys)&&u.IsOrder;
            return View();

        }

        public override ActionResult Details()
        {
            var model = userApp.GetForm(Request["keyValue"]);
            return View(model);

        }

        public ActionResult SetUserLevel(int levelid,string uid) {
            if (userApp.ReSetUserLevel(uid, levelid))
            {//重新设置用户角色后，需要删除原来生成的授权证书，下次访问时会自动生成最新
                var u = userApp.GetForm(uid);
                string imgpath = Request.MapPath("~/upload/shouquan/" + u.F_Account + ".jpg");
                if(System.IO.File.Exists(imgpath))
                    System.IO.File.Delete(imgpath);
                return Success("角色设置成功！");
            }            
            else
                return Error("角色设置失败！");
        }

        public ActionResult IndexYaJin() {
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonYaJin(Pagination pagination)
        {
            var data = new
            {
                rows = userApp.GetListForTop(pagination),
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }

        /// <summary>
        /// 代理商的退还押金操作
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public ActionResult DeleteYaJin(string keyValue)
        {        
            if (userApp.ReturnYaJin(keyValue))
            {//重新设置用户角色后，需要删除原来生成的授权证书，下次访问时会自动生成最新
                var u = userApp.GetForm(keyValue);
                string imgpath = Request.MapPath("~/upload/shouquan/" + u.F_Account + ".jpg");
                if (System.IO.File.Exists(imgpath))
                    System.IO.File.Delete(imgpath);
                return Success("押金退还操作成功，角色重置！");
            }
            else
                return Error("押金退还操作失败！");
        }
    }
}
